import { Body, Controller, Get, Logger, Post } from '@nestjs/common';
import { AppService } from './app.service';
import { SchedulerService } from './service/scheduler.service';

@Controller()
export class AppController {
  constructor(
    private readonly appService: AppService,
    private readonly schedulerService: SchedulerService,
  ) {}

  @Get()
  getHello(): string {
    return this.appService.getHello();
  }

  @Post('start-task')
  startTask(@Body() body: { name: string; seconds: string }) {
    const { name, seconds } = body;
    this.schedulerService.addCronJob(name, seconds, () => {
      console.log(`Task ${name} is running...`, name);
    });

    return { message: '开启定时任务成功' };
  }
}
